<?php
declare (strict_types=1);

namespace app\admin\service\market\ticket;

use app\admin\logic\market\ticket\TicketCoupon as TicketCouponLogic;
use app\common\library\Queue;
use app\common\service\BaseService;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\db\exception\DbException;
use think\Response;

/**
 * 活动券管理
 */
class TicketCoupon extends BaseService
{
    /**
     * @Inject()
     *
     * @var TicketCouponLogic
     */
    protected $ticketCouponLogic;

    /**
     * list
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 23/07/06
     *
     * @param int $ticketId
     * @return Response
     * @throws DbException
     */
    public function list(int $ticketId): Response
    {
        $where = [
            ['ticket_id', '=', $ticketId]
        ];
        // Request::has('title', 'get', true) && $where[] = ['title', 'like', '%' . Request::get('title/s') . '%'];
        $data = $this->ticketCouponLogic->getList($where);

        // 定义字段
        $keyList = [
            'issue_no'      => ListItem::make()->title('活动券期号'),
            'coupon_type'   => ListItem::make('custom')->title('卡券类型'),
            'coupon_info'   => ListItem::make('custom')->title('优惠信息'),
            'effective_day' => ListItem::make()->title('有效期'),
            'create_time'   => ListItem::make()->title('创建时间'),
            'is_receive'    => ListItem::make('custom')->title('可领取状态'),
        ];
        // 设置表单
        $list = new PageList;
        // 表单字段
        $list->pageKey = $keyList;
        $list->datas   = $data;

        return $list->send();
    }

    /**
     * 获取选择活动券列表数据
     *
     * @param int $ticketId
     * @return \eduline\admin\page\interfaces\think\Response
     * @throws DbException
     */
    public function chooseList(int $ticketId)
    {
        $where = [
            ['ticket_id', '=', $ticketId]
        ];
        $data  = $this->ticketCouponLogic->getList($where);

        $list        = new PageList();
        $list->datas = $data;

        return $list->send();
    }

    /**
     * 发送短信任务
     *
     * @param int $ticketId
     * @param int $id
     * @return Response
     */
    public function sendSmsNotify(int $ticketId, int $id)
    {
        Queue::push('EventTicketNotify', [
            'ticket_id' => $ticketId,
            'coupon_id' => $id
        ]);

        return $this->parseToData([], 1, '发送成功');
    }
}
